#include<iostream>
#include<queue>
using namespace std;
const int N = 45;
typedef pair<int, int> PII;
#define x first
#define y second
int dis[N][N], vis[N][N];
char mp[N][N];
int n, m;
int dx[] = { 0,1,0,-1 };
int dy[] = { 1,0,-1,0 };
int dbfs()
{
	dis[0][0] = 1;
	dis[n - 1][m - 1] = 1;
	queue<PII>q1, q2;
	q1.push({ 0,0 });
	q2.push({ n - 1,m - 1 });
	vis[0][0] = 1;
	vis[n - 1][m - 1] = 2;
	while (!q1.empty() && !q2.empty()) {
		bool flag = 1;
		//q1С flag = 1
		int x = 0, y = 0;
		if (q1.size() > q2.size()) {
			flag = 0;
			x = q2.front().x;
			y = q2.front().y;
			q2.pop();
		}
		else {
			x = q1.front().x;
			y = q1.front().y;
			q1.pop();
		}
		for (int i = 0; i < 4; i++) {
			int a = x + dx[i];
			int b = y + dy[i];
			if (a < 0 || b < 0 || a >= n || b >= m || mp[a][b] == '#') {
				continue;
			}
			if (!dis[a][b]) {
				dis[a][b] = dis[x][y] + 1;
				vis[a][b] = vis[x][y];
				if (flag) {
					q1.push({ a,b });
				}
				else {
					q2.push({ a,b });
				}
			}
			else {
				if (vis[a][b] + vis[x][y] == 3) {
					int ans = 0;
					ans = dis[a][b] + dis[x][y];
					return ans;
				}
			}
		}
	}
}
int main()
{
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> mp[i];
	}
	int ret = dbfs();
	cout << ret << endl;
	return 0;
}